package com.ctshk.rpc.hotel.APIUtils;

import com.ctshk.common.model.TokenUser;
import com.ctshk.rpc.hotel.entity.HotelStaticCity;
import com.ctshk.rpc.hotel.entity.HotelStaticCountry;
import com.ctshk.rpc.hotel.entity.HotelStaticProvience;
import com.ctshk.rpc.hotel.entity.NCNBStaticInfo;
import com.ctshk.rpc.hotel.mapper.HotelStaticCityMapper;
import com.ctshk.rpc.hotel.mapper.HotelStaticCountryMapper;
import com.ctshk.rpc.hotel.mapper.HotelStaticProvienceMapper;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.List;

/**
 *
 * 龙腾的XML静态文件解析为Bean
 *
 * */
public class XmlToBeanUtil {
    private static final Integer NO_DELETE = 0;
    private static final Integer YES_DELETE = 1;
    @Autowired
    private HotelStaticCityMapper hotelStaticCityMapper;
    @Autowired
    private HotelStaticProvienceMapper hotelStaticProvienceMapper;
    @Autowired
    private HotelStaticCountryMapper hotelStaticCountryMapper;
    public static NCNBStaticInfo xmlToBean(TokenUser user) throws IOException, JDOMException {
        NCNBStaticInfo ncnbStaticInfo = new NCNBStaticInfo();
        HotelStaticCity hotelStaticCity = new HotelStaticCity();
        HotelStaticProvience hotelStaticProvience = new HotelStaticProvience();
        HotelStaticCountry hotelStaticCountry = new HotelStaticCountry();
        SAXBuilder saxBuilder = new SAXBuilder();
        InputStream is = new FileInputStream(new File("d://ntstatic.xml"));
        Document document = saxBuilder.build(is);
        Element rootElement = document.getRootElement();
        List<Element> children = rootElement.getChildren();
        if(children!=null){
            List<Element> children1 = children.get(2).getChildren();
            for (Element o : children1) {
                List<Element> countrysList = o.getChildren();
                if(countrysList!=null){
                    for (int i = 0; i <countrysList.size() ; i++) {
                        List<Element> countryList = countrysList.get(i).getChildren();
                        Element country = countrysList.get(i);
                        System.out.println("节点名:"+country.getAttribute("CountryId").getName()+"---"+"节点值:"+country.getAttribute("CountryId").getValue());
                        System.out.println("节点名:"+country.getAttribute("CountryName").getName()+"---"+"节点值:"+country.getAttribute("CountryName").getValue());
                        hotelStaticCountry.setId(Long.valueOf(country.getAttribute("CountryId").getValue()));
                        hotelStaticCountry.setCountryName(country.getAttribute("CountryName").getValue());
                        hotelStaticCountry.setIsDeleted(NO_DELETE);
                        hotelStaticCountry.setCreateId(user.getId());
                        hotelStaticCountry.setGmtCreate(LocalDateTime.now());
                        ncnbStaticInfo.getHotelStaticCountry().add(hotelStaticCountry);
                        if(countryList!=null){
                            for (int i1 = 0; i1 < countryList.size(); i1++) {
                                Element Province = countryList.get(i1);
                                System.out.println("节点名:"+Province.getAttribute("ProvinceId").getName()+"---"+"节点值:"+Province.getAttribute("ProvinceId").getValue());
                                hotelStaticProvience.setId(Long.valueOf(Province.getAttribute("ProvinceId").getValue()));
                                if(country.getAttribute("ProvinceName")!=null&&!country.getAttribute("ProvinceName").equals("")){
                                    System.out.println("节点名:"+country.getAttribute("ProvinceName").getName()+"---"+"节点值:"+country.getAttribute("ProvinceName").getValue());
                                    hotelStaticProvience.setProvienceName(country.getAttribute("ProvinceName").getValue());
                                }
                                hotelStaticProvience.setCountryId(hotelStaticCountry.getId());
                                hotelStaticProvience.setCountryName(hotelStaticCountry.getCountryName());
                                hotelStaticProvience.setCreateId(user.getId());
                                hotelStaticProvience.setIsDeleted(NO_DELETE);
                                hotelStaticProvience.setGmtCreate(LocalDateTime.now());
                                ncnbStaticInfo.getHotelStaticProvience().add(hotelStaticProvience);
                                List<Element> provience = countryList.get(i1).getChildren();
                                for (int j = 0; j < provience.size(); j++) {
                                    Element City = provience.get(j);
                                    System.out.println("节点名:"+City.getAttribute("CityId").getName()+"---"+"节点值:"+City.getAttribute("CityId").getValue());
                                    System.out.println("节点名:"+City.getAttribute("CityName").getName()+"---"+"节点值:"+City.getAttribute("CityName").getValue());
                                    hotelStaticCity.setId(Long.valueOf(City.getAttribute("CityId").getValue()));
                                    hotelStaticCity.setCityName(City.getAttribute("CityName").getValue());
                                    hotelStaticCity.setCountryId(hotelStaticCountry.getId());
                                    hotelStaticCity.setCountryName(hotelStaticCountry.getCountryName());
                                    hotelStaticCity.setProvienceId(hotelStaticProvience.getId());
                                    hotelStaticCity.setProvienceName(hotelStaticProvience.getProvienceName());
                                    hotelStaticCity.setIsDeleted(NO_DELETE);
                                    hotelStaticCity.setCreateId(user.getId());
                                    hotelStaticCity.setGmtCreate(LocalDateTime.now());
                                    ncnbStaticInfo.getHotelStaticCity().add(hotelStaticCity);
                                }
                            }
                        }

                    }
                }
            }
        }
        return ncnbStaticInfo;
    }

    public static void main(String[] args) throws IOException, JDOMException {

    }
}

/*{

<?xml version="1.0" encoding="utf-8" ?>
<class>
    <student>
        <firstname>cxx1</firstname>
        <lastname>Bob1</lastname>
        <nickname>stars1</nickname>
        <marks>85</marks>
    </student>
    <student rollno="493">
        <firstname>cxx2</firstname>
        <lastname>Bob2</lastname>
        <nickname>stars2</nickname>
        <marks>85</marks>
    </student>
    <student rollno="593">
        <firstname>cxx3</firstname>
        <lastname>Bob3</lastname>
        <nickname>stars3</nickname>
        <marks>85</marks>
    </student>
</class>
            System.out.println("通过rollno获取属性值:"+child.getAttribute("Data"));
            List<Attribute> attributes = child.getAttributes();
            //打印属性
            for (Attribute attr : attributes) {
                System.out.println(attr.getName()+":"+attr.getValue());
            }
            List<Element> childrenList = child.getChildren();
            System.out.println("======获取子节点-start======");
            for (Element o : childrenList) {
                System.out.println("节点名:"+o.getName()+"---"+"节点值:"+o.getValue());
            }
            System.out.println("======获取子节点-end======");
        }*/